const app = getApp();
const PAYMENT_TYPE = {
    WECHAT: 1,
    BALANCE: 2
};
import Dialog from '../../vant-weapp/dist/dialog/dialog';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        inx: 0,
        checked: false,
        user: null,
        queryString: {
            level_id: 0,
            price: 0.00
        },
        payment: {
            order_id: 0,
            pay_type: 0,
        },
        show: false,
        pactShow: false,
        pact:''
    },
    showPact(){
        this.setData({
            pactShow:!this.data.pactShow
        })
    },
    getList() {
        let _this = this;
        app.loading();
        app.HttpsRequest('level.Level/index').then(ret => {
            if (ret) {
                wx.hideLoading();
                let inx;
                let list = ret.data.list;
                for (let key in list) {
                    list[key].choose = false;
                    if (key == _this.data.inx) {
                        list[key].choose = true;
                        inx = key;
                    }
                }
                _this.setData({
                    list: list,
                    pact: ret.data.pact.detail,
                    user: ret.data.user,
                    'queryString.price': list[inx].price,
                    'queryString.level_id': list[inx].level_id
                });
            }
        })
    },

    /**
     * 设置支付方式
     */
    setPayment(event) {
        this.setData({
            'payment.pay_type': event.detail
        });
    },
    pay() {
        let _this = this;
        if (!_this.data.payment.pay_type) return app.tips('请选择支付方式');
        if (_this.data.payment.pay_type == PAYMENT_TYPE.BALANCE) {
            Dialog.confirm({
                title: '订单支付',
                message: '确认使用余额支付？'
            }).then(() => {
                _this.balancePay();
            }).catch(() => {
                _this.setData({
                    show: !_this.data.show
                });
            });
        }
        if (_this.data.payment.pay_type == PAYMENT_TYPE.WECHAT) {
            console.log(_this.data.queryString)
            app.wechatPay({
                url: 'level.Order/unifiedorder',
                data: _this.data.payment,
                callback: function() {
                    setTimeout(function() {
                        // app.switchTab('/pages/stroke/index');
                    }, 1500);
                },
            });
        }
    },
    balancePay() {
        let _this = this;
        app.HttpsRequest('level.Order/balance', 'POST', _this.data.payment).then(ret => {
            if (ret) {
                app.tips(ret.msg);
                setTimeout(function() {
                    _this.getList();
                }, 1500)
            }
        })
    },
    /**
     * 支付选项--点击事件
     */
    onPaymentClick(event) {
        const {
            name
        } = event.currentTarget.dataset;
        this.setData({
            'payment.pay_type': name
        });
    },
    /**
     * 点击事件--立即升级
     */
    apply() {
        let _this = this;
        if (!_this.data.queryString.level_id) {
            return app.tips('请选择会员等级');
        }
        if (_this.data.user.level.level >= _this.data.list[_this.data.inx].level) {
            return app.tips('您已经是尊贵的' + _this.data.user.level.title);
        }
        if (!_this.data.checked) return app.tips('请仔细阅读协议');
        app.loading();
        app.HttpsRequest('level.Order/create', 'POST', _this.data.queryString).then(ret => {
            if (ret) {
                wx.hideLoading();
                _this.setData({
                    show: true,
                    'payment.order_id': ret.data.order_id
                });
            }
        })
    },
    /**
     * 关闭支付选项
     */
    onClose() {
        this.setData({
            show: false
        });
    },
    /**
     * 阅读协议
     */
    confirmPact(event) {
        this.setData({
            checked: event.detail
        });
    },
    /**
     * 设置选中会员
     */
    setLevel(event) {
        let inx = event.currentTarget.dataset.inx;
        let data = this.data.list;
        for (let key in data) {
            data[key].choose = false;
            if (key == inx) {
                data[key].choose = true;
            }
        }
        this.setData({
            list: data,
            inx: inx,
            'queryString.price': data[inx].price,
            'queryString.level_id': data[inx].level_id
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getList();
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})